package com.hznu.jspFinal.controller;

import com.hznu.jspFinal.entity.Class;
import com.hznu.jspFinal.myBeans.R;
import com.hznu.jspFinal.service.ClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(value = "class",description = "班级接口")
@RequestMapping("/class")
public class ClassController {
    @Autowired
    private ClassService classService;

    @ApiOperation(value = "增加班级")
    @PostMapping("/addOne")
    public R addOne(@RequestBody Class clazz){
        try{
            if (classService.selectByClassName(clazz.getName())!=null){
                return R.error("该班级已经存在");
            }
            classService.insert(clazz);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "查找班级")
    @GetMapping("/findClass/{id}")
    public R findClassByName(@PathVariable Integer id){
        try{
            return R.ok().put("data",classService.selectByPrimaryKey(id));
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "修改班级")
    @PostMapping("/modify")
    public R modify(@RequestBody Class clazz)
    {
        try
        {
            classService.update(clazz);
            return R.ok();
        }catch (Exception e)
        {
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value="删除班级")
    @GetMapping("/delete/{id}")
    public R deleteClass(@PathVariable Integer id){
        try{
            classService.delete(id);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "查找一个专业下的所有班级")
    @GetMapping("/findByMajorId/{id}")
    public R findByDepartmentId(@PathVariable Integer id){
        try{
            List<Class> classses = classService.selectByMajorId(id);
            return R.ok().put("data",classses);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }
}
